package top.jnaw.jee.utils.jpush;

import cn.jiguang.common.TimeUnit;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.report.MessagesResult;
import cn.jpush.api.report.ReceivedsResult;
import cn.jpush.api.report.UsersResult;
import top.jnaw.jee.platform.Consts;
import top.jnaw.jee.utils.Log;

public class Reports {


  public static ReceivedsResult getReport(String msgIds) {
    JPushClient jpushClient = new JPushClient(Consts.JPUSH_MASTER_SECRET,
        Consts.JPUSH_APP_KEY);
    ReceivedsResult result = null;
    try {
      result = jpushClient
          .getReportReceiveds(msgIds);
//      Log.d("Got result - " + result);

    } catch (APIConnectionException e) {
      Log.e("Connection error. Should retry later. ");

    } catch (APIRequestException e) {
      Log.e("Error response from JPush server. Should review and fix it. ");
      Log.i("HTTP Status: " + e.getStatus());
      Log.i("Error Code: " + e.getErrorCode());
      Log.i("Error Message: " + e.getErrorMessage());
    }
    return result;
  }

  public static void testGetUsers() {
    JPushClient jpushClient = new JPushClient(Consts.JPUSH_MASTER_SECRET,
        Consts.JPUSH_APP_KEY);
    try {
      UsersResult result = jpushClient
          .getReportUsers(TimeUnit.DAY, "2018-08-10", 3);
      Log.d("Got result - " + result);

    } catch (APIConnectionException e) {
      Log.e("Connection error. Should retry later. ");

    } catch (APIRequestException e) {
      Log.e("Error response from JPush server. Should review and fix it. ");
      Log.i("HTTP Status: " + e.getStatus());
      Log.i("Error Code: " + e.getErrorCode());
      Log.i("Error Message: " + e.getErrorMessage());
    }
  }

  public static void testGetMessages() {
    JPushClient jpushClient = new JPushClient(Consts.JPUSH_MASTER_SECRET,
        Consts.JPUSH_APP_KEY);
    try {
      MessagesResult result = jpushClient.getReportMessages("293482081");
      Log.d("Got result - " + result);

    } catch (APIConnectionException e) {
      Log.e("Connection error. Should retry later. ");

    } catch (APIRequestException e) {
      Log.e("Error response from JPush server. Should review and fix it. ");
      Log.i("HTTP Status: " + e.getStatus());
      Log.i("Error Code: " + e.getErrorCode());
      Log.i("Error Message: " + e.getErrorMessage());
    }
  }

}

